const apiPath = "http://www.scsoul.top:8080"
const request = require('request');
const timeout = 5000;
console.log("请求路径：" + apiPath);
module.exports = {
    defaultApiPath: apiPath,
    REMOTE_API_RESTAR:{
        code:"501",
        msg:"服务器开始自动修复中，请稍后刷新试试！",
    },
    CALL_REMOTE_API_EXCEPTION:{
        code:"502",
        msg:"远程接口调用异常！服务器开始自动修复中，请稍后刷新试试！",
    },
    get(path, query) {
        return new Promise((resolve, reject) => {
            request.get({
                timeout: timeout,    // 设置超时
                method: 'GET',    //请求方式
                url: this.defaultApiPath + path, //url
                qs: query
            }, (error, response, body) => {
                if (!error && response.statusCode == 200) {
                    resolve(JSON.parse(body))
                } else {
                    reject(error)
                }
            });
        })
    },
    /**
     * 
     * @param {*} path     路径
     * @param {*} form     数据
     * @param {*} useQuery 是否通过url传递参数
     * @returns 
     */
    post(path, form,useQuery = false) {
        return new Promise((resolve, reject) => {
            request.post({
                timeout: timeout,// 设置超时
                method: 'POST',//请求方式
                url: this.defaultApiPath + path, //url
                qs:  useQuery?form:null,
                form: useQuery?null:form,
            }, (error, response, body) => {
                if (!error && response.statusCode == 200) {
                    resolve(JSON.parse(body));
                } else {
                    reject("远程调用异常。"+error)
                }
            });
        })
    }
}